import { ref, onMounted } from 'vue'
import type { Ref } from 'vue'
import { RequestBanner } from '@/api/banner'
import { RequestProductList } from '@/api/product'
import type { IProduct } from '@/types/Types'
import { useRouter } from 'vue-router'
import { RequestMemberLogin ,RequesetHotProduct , RequestMembersignin} from '@/api/index'
import { Toast } from 'vant'
import { useUserStore } from '@/stores/user'
// import sha256 from 'js-sha256'

interface IBanner {
	id: number
	url: string
	content: string
	number: number
}
/**
 * 搜索业务
 */
export const useSearch = () => {
	const searchValue = ref('')
	return { searchValue }
}

/**
 * 轮播业务
 */
export const useBanner = () => {
	const bannerList: Ref<Array<IBanner>> = ref([])
	/**
	 * 获取轮播banner列表
	 */
	const getBannerList =  () => {
		 RequestBanner().then(res=>{
			let { resultCode, resultInfo } = res.data
			if (resultCode == 1) {
				bannerList.value = resultInfo.list
			}
		 })

	}
	
	onMounted(() => {
		getBannerList()
	})
	return {bannerList }
}


/**
 * 商品列表业务
 */
export const useProductList = () => {
	const loading: Ref<boolean> = ref(false)
	const finished: Ref<boolean> = ref(false)

	let productList: Ref<Array<IProduct>> = ref([])
	let currentNo: number = 1 //当前页号

	const getProductList = async (pageNo: number, pageSize: number) => {
		let data = await RequestProductList(pageNo, pageSize)
		let { resultCode, resultInfo } = data
		if (resultCode == 1) {
			// list.value = [...list.value,...resultInfo.list]
			productList.value = productList.value.concat(resultInfo.list)
		}
		// 加载状态结束
		loading.value = false

		if (resultCode == -1) {
			finished.value = true
		}
	}

	/**
	 * 加载更多
	 */
	const onLoad = () => {
		// 第一次加载和组件滚动到底部时，会触发 load 事件并将 loading 设置成 true
		// 注：当数据不在一屏时，会一直触发onLoad
		getProductList(currentNo++, 20)
	}

	/**
	 * 下拉刷新
	 */
	const onRefresh = () => {
		currentNo = 1
		productList.value = []
		finished.value = false
		getProductList(currentNo, 20)
	}

	return {
		onRefresh,
		onLoad,
		productList,
		loading,
		finished,
	}
}

/**
 * 商品详情
 */
export const useProductDetail = () => {
	const router = useRouter()
	const onDetail = (id: number) => {
		router.push({ path: '/detail/' + id })
	}
	return { onDetail }
}

/**
 * 登录业务
 */
export const useMemberLogin = () => {
	const store = useUserStore()
	const router = useRouter()
	const username = ref('')
	const password = ref('')
	const onSubmit = async () => {
		let data = await RequestMemberLogin(username.value, password.value)
		let { resultCode, resultInfo, token } = data
		if (resultCode == 1) {
			// 1.保存用户信息
			store.saveUser({ nick: resultInfo.nick, headerimg: resultInfo.headerimg,})
	const usernick=resultInfo.nick
	const userheaderimg=resultInfo.headerimg
	const username=resultInfo.username
			localStorage.setItem('usernick',usernick)
			localStorage.setItem('userheaderimg',userheaderimg)
			// 2.保存token
			localStorage.setItem('TOKEN', token)

			localStorage.setItem('username', username)

			// // password.value = sha256(password)
			// localStorage.setItem('password', password.value)

			
			// 3.跳转到首页
			router.replace({path:'/home'})
		} else {
			Toast.fail('用户名或密码出错!')
		}
	}
	const onClickLeft = () => {
		history.back()
	}

	return {
		onSubmit,
		onClickLeft,
		username,
		password,
	}
}

/**
 * 注册业务
 */
 export const useMembersignin = () => {
	const store = useUserStore()
	const router = useRouter()
	const username = ref('')
	const password = ref('')
	const nick1 = ref('')
	const headerimg = ref('')

	const onRegisterSubmit = async () => {
		let data = await RequestMembersignin(username.value, password.value,nick1.value,headerimg.value)
		console.log('headerimg123',headerimg.value, 'nick.value' ,nick1.value);
		
		let { resultCode, resultInfo, } = data
		if (resultCode == 1) {
			Toast.success('注册成功!')
			// 3.跳转到首页
			// 定时器
			setTimeout(()=>{
				router.replace({path:'/login'})
			},1000)
		} else {
			Toast.fail('注册失败!')
		}
	}
	const onClickLeft = () => {
		history.back()
	}
	
	return {
		onClickLeft,
		onRegisterSubmit,
		username,
		password,
		nick1,
		headerimg
	}
}

 

/**
 * 热门商品
 */
export const useHotProduct = ()=>{
	let hotList = ref([])
	const getHotProduct = ()=>{
		RequesetHotProduct().then(res=>{
			let {resultCode,resultInfo} = res.data
			if(resultCode == 1){
				hotList.value = resultInfo.list
			}
		})
	}
	onMounted(()=>{
		getHotProduct()
	})
	return {hotList}
}
